package com.example.project.projectspringcloud.controller.front.order;

import com.example.project.projectspringcloud.entity.RespBody;
import com.example.project.projectspringcloud.entity.order.OrderCombo;
import com.example.project.projectspringcloud.entity.sys.Area;
import com.example.project.projectspringcloud.service.order.OrderComboService;
import com.example.project.projectspringcloud.service.sys.SysAreaService;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/combo")
public class ComboController {

    @Autowired
    private OrderComboService orderComboService;

    @Autowired
    private SysAreaService sysAreaService;

    @ApiOperation(value = "套餐选择页面", notes = "返回套餐选择页面")
    @GetMapping(value = "/selector")
    public String selector(@RequestParam(value = "city", required = false) Integer city, Model model) throws Exception {
        // 城市信息
        Area entity = new Area();
        entity.setIsOpen(1);
        List<Area> areas = sysAreaService.list(entity, null);
        model.addAttribute("areas", areas);

        try {
            OrderCombo orderCombo = new OrderCombo();
            orderCombo.setCity(city);
            List<OrderCombo> comboColumnList = orderComboService.columnList(orderCombo, 1);
            comboColumnList.forEach(t -> {
                orderCombo.setValue(t.getValue());
                t.setGroups(orderComboService.columnList(orderCombo, 2));
            });
            comboColumnList.forEach(t -> {
                t.getGroups().forEach(e -> {
                    orderCombo.setValue(t.getValue());
                    orderCombo.setGroup(e.getGroup());
                    e.setTags(orderComboService.columnList(orderCombo, 3));
                });
            });

            model.addAttribute("city", city);

            model.addAttribute("comboColumnList", comboColumnList);
            model.addAttribute("comboGroups", comboColumnList.get(0).getGroups());
        } catch (Exception e) {
            model.addAttribute("error", "该城市暂时没有套餐信息");
        }
        return "m/content/order/order-combo-selector";
    }

    @ApiOperation(value = "API获取套餐信息", notes = "返回套餐信息")
    @GetMapping(value = "/api/list")
    @ResponseBody
    public RespBody list(@RequestParam("city") Integer city) {
        if (city == null) {
            return RespBody.error("请选择城市!");
        }
        try {
            OrderCombo orderCombo = new OrderCombo();
            orderCombo.setCity(city);
            List<OrderCombo> comboColumnList = orderComboService.columnList(orderCombo, 1);
            comboColumnList.forEach(t -> {
                orderCombo.setValue(t.getValue());
                t.setGroups(orderComboService.columnList(orderCombo, 2));
            });
            comboColumnList.forEach(t -> {
                t.getGroups().forEach(e -> {
                    orderCombo.setValue(t.getValue());
                    orderCombo.setGroup(e.getGroup());
                    e.setTags(orderComboService.columnList(orderCombo, 3));
                });
            });

            Map combo = Maps.newHashMap();
            combo.put("comboColumnList", comboColumnList);
            combo.put("comboGroups", comboColumnList.get(0).getGroups());
            return RespBody.ok(combo);
        } catch (Exception e) {
            return RespBody.error("获取套餐信息失败!");
        }
    }

    @ApiOperation(value = "获取城市首个套餐, 作为默认值", notes = "获取城市首个套餐, 作为默认值")
    @GetMapping(value = "/get")
    @ResponseBody
    public RespBody get(@RequestParam("city") Integer city) {
        if (city == null) {
            return RespBody.error("请选择城市!");
        }
        try {
            OrderCombo orderCombo = new OrderCombo();
            orderCombo.setCity(city);

            return RespBody.ok(orderComboService.get(orderCombo));
        } catch (Exception e) {
            return RespBody.error("获取套餐信息失败!");
        }
    }
}
